/*
 * This file is part of ksPlatform
 * 
 * Copyright (c) 2014-2015
 * 
 * This file is dual-licensed under the AGPLv3 (http://www.gnu.org/licenses/agpl-3.0.html) 
 * 
 * If you are unsure which license is appropriate for your use, please contact the sales department
 * at 97041437@qq.com.
 */

package cn.com.ksplatform.modules.system.auth.usermanager.service;

import javax.transaction.Transactional;

import org.springframework.stereotype.Repository;

import cn.com.ksplatform.base.service.BaseService;
import cn.com.ksplatform.modules.system.auth.usermanager.model.PlatUser;

/**
 * ClassName:PlatUserService <br/>
 * Date: 2015年5月8日 下午4:28:49 <br/>
 * 
 * @author Mr.Luoxing
 * @version 1.0
 */
@Repository("system.modules.system.PlatUserService")
public class PlatUserService extends BaseService {

	@Transactional
	public PlatUser save(PlatUser user) {
		String id = user.save();
		user.setUserId(id);
		return user;
	}

	@Transactional
	public PlatUser update(PlatUser user) {
		return user.update();
	}
	
	@Transactional
	public PlatUser updateUserOnly(PlatUser user) {
		String hql = "update PlatUser u set  u.enabled = ? , u.other = ? where u.userId = ?";
		int r = hibernateDAO.executeUpdate(hql, new Object[]{user.getEnabled(),user.getOther(),user.getUserId()});
		return r > 0 ?user:null;
	}
	
	@Transactional
	public PlatUser delete(PlatUser user) {
		return user.delete();
	}

	@Transactional
	public PlatUser saveOrUpdate(PlatUser user) {
		if(user.getUserId() != null){
			return updateUserOnly(user);
		}
		return user.saveOrUpdate();
	}
	
	@Transactional
	public PlatUser queryUserByUserName(String userName){
		return (PlatUser) super.uniqueResult(PlatUser.class, "userName", userName);
	}
	
	@Transactional
	public boolean updatePwd(String opwd, String npwd,String userName) {
		String hql = "update PlatUser u set u.passWord = ? where u.passWord = ? and u.userName = ?";
		int r = hibernateDAO.executeUpdate(hql, new Object[]{npwd,opwd,userName});
		return r > 0;
	}
}

